package com.khyr.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.dao.LoginInfoDao;
import com.khyr.entity.LoginInfo;
import com.khyr.service.LoginInfoService;
import com.khyr.vo.LoginInfoVo;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 宋一恒
 * @version 1.0
 */
@Service
public class LoginInfoServiceImpl implements LoginInfoService {
    @Autowired
    private LoginInfoDao loginInfoDao;

    @Override
    public R<Page<LoginInfo>> selectAllLoginInfoByCond(LoginInfoVo loginInfoVo) {
        //分页
        Page<LoginInfo> page = new Page<>(loginInfoVo.getPageNum(), loginInfoVo.getPageSize());
        //模糊
        QueryWrapper<LoginInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(loginInfoVo.getTitle())){
            queryWrapper.like("title", loginInfoVo.getTitle());
        }
        if (StringUtils.hasText(loginInfoVo.getOperName())){
            queryWrapper.like("oper_name", loginInfoVo.getOperName());
        }
        if (StringUtils.hasText(loginInfoVo.getStatus())){
            queryWrapper.like("status", loginInfoVo.getStatus());
        }
        if (StringUtils.hasText(loginInfoVo.getBusinessType())){
            queryWrapper.like("business_type", loginInfoVo.getBusinessType());
        }
        if (StringUtils.hasText(loginInfoVo.getBeginTime())&& StringUtils.hasText(loginInfoVo.getEndTime())){
            queryWrapper.between("create_time", loginInfoVo.getBeginTime(), loginInfoVo.getEndTime());
        }
        Page<LoginInfo> PageInfo = loginInfoDao.selectPage(page, queryWrapper);
        if (PageInfo != null){
            return new R<>(200,"查询成功", PageInfo);
        }
        return new R<>(500,"查询失败", null);
    }

    @Override
    public R<Integer> deleteLoginInfoByLoginInfoIds(List<Long> ids) {
        if (ids != null){
            int delete = loginInfoDao.deleteLoginInfoByLoginInfoIds(ids);
            if (delete > 0){
                return new R<>(200,"删除成功", delete);
            }
        }
        return new R<>(500,"删除失败", null);
    }

    @Override
    public R<Integer> clearLoginInfo() {
        int i = loginInfoDao.clearLoginInfo();
        if (i > 0){
            return new R<>(200,"清除成功", i);
        }
        return new R<>(500,"失败",null);
    }
}
